package Yangtz.Cs.Manager.Controller;

import Yangtz.Cs.Entity.BookBean;
import Yangtz.Cs.Service.BookService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/addBookServlet")
public class addBookServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//post参数在body里面
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        System.out.println("null");

        //writer一定要在这三个之后，不然会乱码
        PrintWriter writer = response.getWriter();
        //输入流
        InputStream inputStream;
        //判断是否多段数据.（只有是多段的数据，才是文件上传的）
        if (ServletFileUpload.isMultipartContent(request)) {
            //创建 FileItemFactory 工厂实现类
            FileItemFactory fileItemFactory = new DiskFileItemFactory();
            //.创建用于解析上传数据的工具类
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
            try {
                List<FileItem> fileItems = servletFileUpload.parseRequest(request);
                BookBean book = new BookBean();
                for (FileItem fileItem : fileItems) {
                    if (fileItem.isFormField())//普通字段表单
                    {
                        //属性名，及key名字
                        String fieldName = fileItem.getFieldName();
                        System.out.println(fieldName);
                        switch (fieldName) {
                            case "bkID":
                                book.setBkID(fileItem.getString("UTF-8"));
                                break;
                            case "bkCode":
                                book.setBkCode(fileItem.getString("UTF-8"));
                                break;
                            case "bkName":
                                book.setBkName(fileItem.getString("UTF-8"));
                                break;
                            case "bkAuthor":
                                book.setBkAuthor(fileItem.getString("UTF-8"));
                                break;
                            case "bkPress":
                                book.setBkPress(fileItem.getString("UTF-8"));
                                break;
                            case "bkPressDate":
                                book.setBkPressDate(fileItem.getString("UTF-8"));
                                break;
                            case "bkISBN":
                                book.setBkISBN(fileItem.getString("UTF-8"));
                                break;
                            case "bkLanguage":
                                book.setBkLanguage(fileItem.getString("UTF-8"));
                                break;
                            case "bkPages":
                                book.setBkPages(fileItem.getString("UTF-8"));
                                break;
                            case "bkPrice":
                                book.setBkPrice(fileItem.getString("UTF-8"));
                                break;
                            case "bkDateIn":
                                book.setBkDateIn(fileItem.getString("UTF-8"));
                                break;
                            case "bkBrief":
                                book.setBkBrief(fileItem.getString("UTF-8"));
                                break;
                            default:
                                throw new IllegalStateException("Unexpected value: " + fieldName);
                        }
                    } else {
                        //获取文件表单属性名:
                        System.out.println(fileItem.getFieldName());
                        //只能有一个/ 这样访问的才是tomcat目录，
                        book.setBkPic("/BookImag/" + book.getBkID() + ".png");
                        //获取图片名
                        System.out.println(fileItem.getName());
                    }
                }

                if (new BookService().insert(book)) {
                    writer.println("添加图书成功");
                    List<BookBean> bookList = new ArrayList<>();
                    bookList.add(book);
                    request.getSession().setAttribute("add_book", bookList);
                } else {
                    writer.println("添加失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
